package org.zjx.core;

import lombok.Data;
import lombok.experimental.Accessors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetSocketAddress;

@Data
@Accessors(chain = true)
public class SocketConfig {
    private static final Logger logger = LoggerFactory.getLogger(SocketConfig.class);

    private boolean master = false;
    private boolean debugEnabled = false;
    private String serverHost = "localhost";
    private int port = 8888;
    private long heartbeatInterval = 15_000;
    private long heartbeatTimeout = 45_000;
    private int clientPort = -1;

    private int maxConnections = 300; // 作为服务器允许客户端最大连接数

    private int poolMaxTotal = 50;            // 连接池最大连接数
    private long poolMaxIdleTime = 60_000;     // 连接池最大空闲时间（毫秒）

    /** RPC请求超时时间（毫秒，默认：5秒） */
    private long requestTimeout = 5_000;

    /** 连接失败后的初始重连间隔（毫秒，默认：5秒） */
    private long reconnectInterval = 5_000;

    /** 最大重连次数（默认：无限制，设为-1表示不限制） */
    private int maxReconnectAttempts = -1;

    // 新增获取客户端地址的方法
    public InetSocketAddress getClientAddress() {
        if (clientPort > 0) {
            return new InetSocketAddress(serverHost, clientPort);
        }
        return null;
    }

    public void validate() {
        if (port <= 0 || port > 65535) {
            logger.error("Invalid port number: {}", port);
            throw new IllegalArgumentException("Invalid port number");
        }
        // ... 其他验证 ...
    }
}